//
//  WWCharmDate.m
//  WhatWeather
//
//  Created by byn on 16/3/20.
//  Copyright © 2016年 byn. All rights reserved.
//

#import "WWCharmDate.h"

@interface WWCharmDate ()

@property (nonatomic ,strong) NSDateComponents *dateComponents;
@property (nonatomic ,strong) NSDate *date;

@end

@implementation WWCharmDate

+ (WWCharmDate *)createDateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minite:(NSInteger)minite second:(NSInteger)second calendarIdentifier : (NSString *)identifier {
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.year = year;
    dateComponents.month = month;
    dateComponents.day = day;
    dateComponents.hour = hour;
    dateComponents.minute = minite;
    dateComponents.second = second;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:identifier];
    WWCharmDate * charmDate = [[WWCharmDate alloc] init];
    charmDate.dateComponents = dateComponents;
    charmDate.date = [calendar dateFromComponents:dateComponents];
    
    return charmDate;
}

+ (WWCharmDate *)createDateWithDataStr: (NSString *)dateStr withDataFormatter : (NSString *)dataFormatter {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = dataFormatter;
    
    WWCharmDate *charmDate = [[WWCharmDate alloc] init];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    charmDate.date = date;
    NSDateComponents *dateComponent = [charmDate buildDateComponents];
    charmDate.dateComponents = dateComponent;
    
    return charmDate;
}

- (NSDateComponents *)buildDateComponents {
    
    NSInteger timeFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponent = [calendar components:timeFlag fromDate:self.date];
    
    return dateComponent;
}

- (void)addDays : (NSInteger)days {
    [self addCalendarUnits:NSCalendarUnitDay withValue:days];
}

- (void)addYear : (NSInteger)year {
    [self addCalendarUnits:NSCalendarUnitYear withValue:year];
}

- (void)addMonth : (NSInteger)month {
    [self addCalendarUnits:NSCalendarUnitMonth withValue:month];
}

- (void)addHour : (NSInteger)hour {
    [self addCalendarUnits:NSCalendarUnitHour withValue:hour];
}

- (void)addMinite : (NSInteger)minite {
    [self addCalendarUnits:NSCalendarUnitMinute withValue:minite];
}

- (void)addSecond : (NSInteger)second {
    [self addCalendarUnits:NSCalendarUnitSecond withValue:second];
}

- (void )addCalendarUnits : (NSCalendarUnit)unit  withValue : (NSInteger)value {
    
    self.date = [[NSCalendar currentCalendar] dateByAddingUnit:unit value:value toDate:self.date options: NSCalendarWrapComponents];
    self.dateComponents = [self buildDateComponents];
}

- (NSInteger )getYear {
    return self.dateComponents.year;
}
- (NSInteger)getMonth {
    return self.dateComponents.month;
}
- (NSInteger)getDay {
    return self.dateComponents.day;
}
- (NSInteger)getWeekDay {
    return self.dateComponents.weekday;
}
- (NSInteger)getHours {
    return self.dateComponents.hour;
}
- (NSInteger)getMinite {
    return self.dateComponents.minute;
}
- (NSInteger)getSecond{
    return self.dateComponents.second;
}

@end












































